//
//  ThemeCellModelToolB.m
//  ZZBlue
//
//  Created by stonesMonkey on 2017/10/12.
//  Copyright © 2017年 stonesMonkey. All rights reserved.
//

#import "ThemeCellModelToolB.h"
#define kThemeModelKey @"themeModelKeyB"
#define themeModelListPath @"themeModelListPathB.plist"

@implementation ThemeCellModelToolB


+(instancetype)allocWithZone:(struct _NSZone *)zone {
    
    static ThemeCellModelToolB *obj = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        obj = [super allocWithZone:zone];
        
    });
    
    return obj;
}
// 保存
- (void)save {
    
    //准备路径:
    NSString *path = NSHomeDirectory();
    NSLog(@"%@",path);
    
    path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",themeModelListPath]];
    //1:准备存储数据的对象
    NSMutableData *data = [NSMutableData data];
    //2:创建归档对象
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    //3:开始归档
    [archiver encodeObject:self.group forKey:kThemeModelKey];
    //4:完成归档
    [archiver finishEncoding];
    //5:写入文件当中
    BOOL result = [data writeToFile:path atomically:YES];
    if (result) {
        
        // 写入成功
    }
    
}

- (ZZThemeCellModelGroup *)group {
    
    if (!_group) {
        
        // 从数据中拿
        NSString *path = NSHomeDirectory();
        path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",themeModelListPath]];
        NSData *myData = [NSData dataWithContentsOfFile:path];
        //创建反归档对象
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:myData];
        //反归档
        ZZThemeCellModelGroup* listGroupModel = [unarchiver decodeObjectForKey:kThemeModelKey];
        //完成反归档
        [unarchiver finishDecoding];
        
        if(listGroupModel == nil) { // 如果没有则数据初始化
            
            listGroupModel = [[ZZThemeCellModelGroup alloc] init];
            
            ZZThemeCellModel *model1 = [ZZThemeCellModel new];
            model1.title = @"欢乐海岸(主题样式一B)";
            model1.modelStyle = 0;
            ZZColorRGBA *model1_color1 = [ZZColorRGBA colorRGBAWithR:255.0 G:0 B:0 A:1];
            ZZColorRGBA *model1_color2 = [ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1];
            model1.pinLv = 0.5;
            model1.lightValue = 0.5;
             [model1.colors addObject:model1_color2];
            [model1.colors addObject:model1_color1];
           
            [listGroupModel.models addObject:model1];
            
            
            ZZThemeCellModel *model2 = [ZZThemeCellModel new];
            model2.title = @"年复一年(主题样式二B)";
            ZZColorRGBA *model2_color1 = [ZZColorRGBA colorRGBAWithR:102 G:244 B:108 A:1];
            ZZColorRGBA *model2_color2 = [ZZColorRGBA colorRGBAWithR:95 G:255.0 B:255.0 A:1];
            ZZColorRGBA *model3_color3 = [ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:0 A:1];
            ZZColorRGBA *model4_color4 = [ZZColorRGBA colorRGBAWithR:255.0 G:255.0 B:255.0 A:1];
            model2.modelStyle = 1;

            [model2.colors addObject:model3_color3];
            [model2.colors addObject:model4_color4];
            [model2.colors addObject:model2_color1];
            [model2.colors addObject:model2_color2];
           
            [listGroupModel.models addObject:model2];
            
            ZZThemeCellModel *model3 = [ZZThemeCellModel new];
            model3.title = @"浪漫温柔(主题样式三B)";
            ZZColorRGBA *model3_color1 = [ZZColorRGBA colorRGBAWithR:226 G:61 B:224 A:1];
            ZZColorRGBA *model3_color2 = [ZZColorRGBA colorRGBAWithR:239 G:168 B:240.0 A:1];
            
            model3.modelStyle = 2;
             [model3.colors addObject:model3_color2];
            [model3.colors addObject:model3_color1];
           
            [listGroupModel.models addObject:model3];
            
            ZZThemeCellModel *model4 = [ZZThemeCellModel new];
            model4.title = @"我爱永恒(主题样式四B)";
            ZZColorRGBA *model4_color1 = [ZZColorRGBA colorRGBAWithR:226 G:61 B:224 A:1];
            ZZColorRGBA *model4_color2 = [ZZColorRGBA colorRGBAWithR:239 G:168 B:240.0 A:1];
            
            model4.modelStyle = 3;
                  [model4.colors addObject:model4_color2];
            [model4.colors addObject:model4_color1];
      
            [listGroupModel.models addObject:model4];
            
            ZZThemeCellModel *model5 = [ZZThemeCellModel new];
            model5.title = @"我爱永恒(主题样式五B)";
            ZZColorRGBA *model5_color1 = [ZZColorRGBA colorRGBAWithR:226 G:61 B:224 A:1];
            ZZColorRGBA *model5_color2 = [ZZColorRGBA colorRGBAWithR:239 G:168 B:240.0 A:1];
            
            model5.modelStyle = 4;
            [model5.colors addObject:model5_color1];
            [model5.colors addObject:model5_color2];
            [listGroupModel.models addObject:model5];
            
            _group = listGroupModel;
            // 归档
            //准备路径:
            NSString *path = NSHomeDirectory();
            NSLog(@"%@",path);
            
            path = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",kCFURLPathKey]];
            //1:准备存储数据的对象
            NSMutableData *data = [NSMutableData data];
            //2:创建归档对象
            NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
            //3:开始归档
            [archiver encodeObject:listGroupModel forKey:themeModelListPath];
            //4:完成归档
            [archiver finishEncoding];
            //5:写入文件当中
            BOOL result = [data writeToFile:path atomically:YES];
            if (result) {
                
                // 写入成功
            }
            
            [self save];
        } else {
            
            _group = listGroupModel;
        }
    }
    
    return _group;
}


@end
